package com.tsd.core.shiro.helper;

import com.tsd.core.shiro.IShiroConst;
import net.sf.ehcache.CacheManager;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.ehcache.EhCacheManager;

/**
 * @ClassName: ShiroAuthorizationInfoHelper
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/7/18 13:50
 * @Version: 1.0
 */
public class ShiroAuthorizationInfoHelper {

    private static EhCacheManager ehCacheManager;
    private static CacheManager cacheManager;
    private static Cache<String, SimpleAuthorizationInfo> simpleAuthorizationInfoCache;

    public static EhCacheManager getEhCacheManager() {
        return ehCacheManager;
    }

    public static CacheManager getCacheManager() {
        return cacheManager;
    }

    public static Cache<String, SimpleAuthorizationInfo> getSimpleAuthorizationInfoCache() {
        return simpleAuthorizationInfoCache;
    }

    public static void setEhCacheManager(EhCacheManager ehCacheManager) {
        ShiroAuthorizationInfoHelper.ehCacheManager = ehCacheManager;
        ShiroAuthorizationInfoHelper.cacheManager = ehCacheManager.getCacheManager();
        simpleAuthorizationInfoCache = ehCacheManager.getCache(IShiroConst.CACHE_NAME);
    }

    public static void put(String key, SimpleAuthorizationInfo info) {
        simpleAuthorizationInfoCache.put(key, info);
    }

    public static SimpleAuthorizationInfo get(String key) {
        return simpleAuthorizationInfoCache.get(key);
    }


    public static void clearCache() {
        Cache<String, SimpleAuthorizationInfo> cache = ehCacheManager.getCache(IShiroConst.CACHE_NAME);
        if (cache.size() > 0) {
            cache.clear();
        }
    }
}
